package com.lucky.auth_boot.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lucky.auth_boot.exception.BusinessException;
import com.lucky.auth_boot.mapper.PirateMapper;
import com.lucky.auth_boot.pojo.dto.PirateDTO;
import com.lucky.auth_boot.pojo.dto.UserDTO;
import com.lucky.auth_boot.pojo.po.PiratePO;
import com.lucky.auth_boot.pojo.vo.PirateUserVO;
import com.lucky.auth_boot.service.PirateService;
import com.lucky.auth_boot.utils.ConvertUtils;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PirateServiceImpl implements PirateService {

    @Resource
    private PirateMapper pirateMapper;

    public List<PiratePO> queryPirateDoaminList() {
        return pirateMapper.selectList(null);
    }

    @Override
    public PiratePO queryPirateDomainById(Integer id) {
        QueryWrapper<PiratePO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", id);
        return pirateMapper.selectOne(queryWrapper);
    }

    @Override
    public Integer deletePirateDomainById(Integer id) {
        try {
            QueryWrapper<PiratePO> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id", id);
            return pirateMapper.delete(queryWrapper);
        } catch (Exception e) {
            throw new BusinessException(500,"操作失败");
        }
    }

    @Override
    public Integer deletePirateDomainByIds(Integer id) {
        try {
            UserDTO userDTO = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            PiratePO piratePO = new PiratePO();
            piratePO.setUpdater(userDTO.getId());
            piratePO.setUpdateTime(new Date());
            piratePO.setIsDelete(1);

            QueryWrapper<PiratePO> wrapper = new QueryWrapper<>();
            wrapper.eq("id", id);

            return pirateMapper.update(piratePO, wrapper);
        } catch (Exception e) {
            throw new BusinessException(500,"操作失败");
        }
    }

    @Override
    public Integer updatePirateDomain(PiratePO piratePO) {
        try {
            UserDTO userDTO = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

            piratePO.setUpdater(userDTO.getId());
            piratePO.setUpdateTime(new Date());
            piratePO.setCreator(null);
            piratePO.setCreateTime(null);
            piratePO.setIsDelete(null);

            QueryWrapper<PiratePO> wrapper = new QueryWrapper<>();
            wrapper.eq("id", piratePO.getId());

            return pirateMapper.update(piratePO,wrapper);
        } catch (Exception e) {
            throw new BusinessException(500,"操作失败");
        }
    }

    @Override
    public Integer addPirateDomain(PiratePO piratePO) {
        UserDTO userDTO = (UserDTO) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        piratePO.setId(null);
        piratePO.setCreator(userDTO.getId());
        piratePO.setCreateTime(new Date());
        piratePO.setIsDelete(0);

        try {
            return pirateMapper.insert(piratePO);
        } catch (Exception e) {
            throw new BusinessException("操作失败");
        }
    }

    @Override
    public Page<PirateUserVO> queryPirateUrlListPage(Pageable pageable, PirateDTO pirateDTO) {
        Page<PirateUserVO> page = new Page<>(pageable.getPageNumber(), pageable.getPageSize());
//        ConvertUtils.emptyToNull(pirateDTO);
        return pirateMapper.queryPirateUrlListPage(page,pirateDTO);
    }

    @Override
    public PirateUserVO queryPirateOneById(Integer id) {
        return pirateMapper.queryPirateOneById(id);
    }
}
